//
//  HJOrderedDictionary.m
//  TJPageControllTest
//
//  Created by lx on 2023/11/18.
//

#import "HJOrderedDictionary.h"

@interface HJOrderedDictionary ()

@property (nonatomic, strong) NSMutableArray *orderedKeys;
@property (nonatomic, strong) NSMutableDictionary *dictionary;

@end


@implementation HJOrderedDictionary


+ (instancetype)dictionary {
    return [[self alloc] init];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _orderedKeys = [NSMutableArray array];
        _dictionary = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
    if (![self.orderedKeys containsObject:aKey]) {
        [self.orderedKeys addObject:aKey];
    }
    [self.dictionary setObject:anObject forKey:aKey];
}

- (NSArray *)allKeys {
    return self.orderedKeys.copy;
}

- (NSArray *)allValues {
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *key in self.orderedKeys) {
        [array addObject:[self objectForKey:key]];
    }
    return array.copy;
}

- (id)objectForKey:(id)aKey {
    return [self.dictionary objectForKey:aKey];
}

- (void)removeObjectForKey:(id)aKey {
    if (![self.orderedKeys containsObject:aKey]) return;
    [self.orderedKeys removeObject:aKey];
    [self.dictionary removeObjectForKey:aKey];
}

- (NSUInteger)count {
    return [self.orderedKeys count];
}

- (NSString *)description {
    NSMutableString *description = [NSMutableString string];
    [description appendString:@"{\n"];
    for (id key in self.orderedKeys) {
        id value = [self.dictionary objectForKey:key];
        [description appendFormat:@"  %@ = %@;\n", key, value];
    }
    
    [description appendString:@"}"];
    return description;
}

@end
 
 
